NOTE:- For the best experience, this 3D simulation is highly visual. We recommend opening it on a desktop or laptop.
Choose which applications to include in the simulation.
Basic ModeAdvanced Mode
Context Switching
What is Context Switching?
Context switching is the process the CPU follows to change from one task (or process) to another. It is the process of saving and restoring the state (context) of a CPU so that multiple processes can share a single CPU efficiently. It's a fundamental feature of multitasking operating systems, allowing multiple processes to share a single CPU core, creating the illusion of simultaneous execution.
It occurs in multitasking operating systems when the CPU switches from executing one process to another.
The Process Step-by-Step
Interrupt/System Call: An event, such as a timer expiring or a request for I/O, triggers the need for a switch.
Save Context: The OS saves the current state of the running process (CPU registers, program counter, stack pointer) into its Process Control Block (PCB). The PCB is a data structure in the kernel that stores all information about a process.
Scheduler: The OS's scheduler runs to select the next process from the ready queue to execute.
Load Context: The OS loads the state of the newly selected process from its PCB into the CPU's registers.
Resume Execution: The CPU is now "switched," and it resumes executing the new process from the point it was last stopped.
Example with YouTube & Netflix
Suppose you open a tab with YouTube. A new PCB (Process Control Block) is created.
PCB contains → Process ID, Program Counter, Registers, Process State, etc.
The PCB is stored in RAM, and CPU executes it. Now YouTube is in a Running State. Its current state is stored in the Program Counter and Registers.
Then you open Netflix → Another PCB is created for Netflix.
The CPU switches to the Netflix process. This is called Context Switching / Multitasking.
Switching Back
When you switch from YouTube → Netflix: YouTube’s state (data, registers, PC) is stored in its PCB. Netflix’s state is restored from its PCB.
When you again switch to YouTube: Netflix’s state is saved in its PCB. YouTube’s PCB is restored (program counter + registers).
About CPU Registers
CPU registers are small, extremely fast storage locations within the CPU itself. They are crucial for any computation. The values in these registers change constantly as a program executes its instructions.
General Purpose (R1, R2): Used to temporarily store data for arithmetic and logic operations.
Program Counter (PC): Holds the memory address of the next instruction to be executed. It increments automatically after each instruction.
Stack Pointer (SP): Points to the top of the process's stack, which is used for managing function calls and local variables.
Because each process has its own unique set of register values at any given moment, these values are the most critical part of the "context" that must be saved and restored during a switch.
The "Cost" of Context Switching
Context switching is pure overhead; no useful user-level work is done during the switch. The time spent switching is known as the "cost".
A conceptual formula for this cost is:
Switch Time = Save Time + Schedule Time + Load Time
For example: If a process runs for 10ms and a context switch takes 1ms, the total time for that cycle is 11ms. The CPU efficiency is 10ms / 11ms ≈ 90.9%. If switches become too frequent, this overhead can significantly degrade system performance.
Context Switching Visualizer
1. Process Execution: The active process moves to the CPU.
2. Switch Initiated: A request to switch to another process is made.
3. Save State: The current context is saved to its PCB as the process moves away.
4. Load State: The new context is loaded from its PCB as the new process moves in.
5. New Process Execution: The CPU resumes with the new process.